<template>
  <div id="registerBusiness">
    <el-dialog
      title="注册商家"
      width="30%"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
    >
      <el-form
        ref="businessListReset"
        :model="businessList"
        :rules="businessRules"
        label-position="right"
        label-width="100px"
      >
        <el-form-item label="店铺名字" prop="username">
          <el-input
            v-model="businessList.username"
            placeholder="请输入店铺名字"
          ></el-input>
        </el-form-item>
        <el-form-item label="店铺描述" prop="desc">
          <el-input
            v-model="businessList.desc"
            placeholder="请输入店铺描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="店铺Id" prop="fkUserUseridMerchantMerchantid">
          <el-input
            disabled
            v-model="businessList.fkUserUseridMerchantMerchantid"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose('businessListReset')">取 消</el-button>
        <el-button type="primary" @click="regbusiness('businessListReset')"
          >注 册</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "BusinessRegister",
  props: ["dialogVisible"],
  data() {
    return {
      //注册商家数据
      businessList: {
        //店铺描述
        desc: null,
        //用户id
        fkUserUseridMerchantMerchantid: null,
        //店铺名字
        username: null,
      },
      businessRules: {
        username: [
          { required: true, message: "请输入店铺名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" },
        ],
        desc: [{ required: true, message: "请输入店铺描述", trigger: "blur" }],
        fkUserUseridMerchantMerchantid: [{ require: true ,type:'number'}],
      },
    };
  },
  mounted() {
    this.businessList.fkUserUseridMerchantMerchantid = this.$store.state.userid;
  },
  watch: {
    "$store.state.username": function () {
      this.businessList.fkUserUseridMerchantMerchantid =
        this.$store.state.userid;
    },
  },
  methods: {
    //关闭弹出框
    handleClose(rule) {
      this.$emit("dialogClose", false);
      this.$refs[rule].resetFields();
    },
    // 注册商家
    regbusiness(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post("/tMerchant/registemerchant", this.businessList)
            .then((res) => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  duration: 1200,
                  message: "恭喜您，注册成功",
                  onClose(){
                    this.handleClose();
                  }
                });
              }else{
                this.$message({
                  type: "error",
                  duration: 1200,
                  message: res.data.message,
                });
              }
            });
        } else {
          this.$message({
            type: "warning",
            duration: 2000,
            message: "请把正确填写数据",
          });
          return false;
        }
      });
    },
  },
};
</script>

<style>
</style>